package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.Util.SendSMS;
import com.lnking.lnkingplace.model.entity.Message;
import com.lnking.lnkingplace.mapper.MessageMapper;
import com.lnking.lnkingplace.service.IMessageService;
import com.lnking.lnkingplace.service.IUserService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-11-24
 */
@Service
public class MessageServiceImpl extends CustomServiceImpl<MessageMapper, Message> implements IMessageService {


    @Autowired
    IUserService userService;

    @Override
    public void send(String phone) throws Exception {
        if (StringUtils.isEmpty(phone)) {
            throw new Exception("手机号码不能为空");
        }
        if (phone.length() != 11){
            throw new Exception("手机号码不正确");
        }
        userService.findPhone(phone);

        Random random = new Random();
        int code = random.nextInt(9999 - 1000 + 1) + 1000;
        System.out.println(code);

        String messageCode = DigestUtils.sha1Hex(String.valueOf(code));
        Message message = phone(phone);
        if(message ==null){
            message = new Message();
            message.setCode(messageCode);
            message.setPhone(phone);
            message.setCreateTime(LocalDateTime.now());
            message.setNumber(1);
            save(message);
        }else{
            Duration duration = Duration.between(message.getCreateTime(),LocalDateTime.now());
            if ((duration.toMinutes()<=1)) {
                throw new Exception("请等待一分钟再发起请求");
            }

            if (message.getNumber()>=5) {
                throw new Exception("今日发送验证码次数已超过了5次");
            }
            if(LocalDateTimeUtil.isOnToday(message.getCreateTime())){
                message.setNumber(message.getNumber()+1);
            }else{
                message.setNumber(1);
            }
            message.setCode(messageCode);
            message.setCreateTime(LocalDateTime.now());
            getBaseMapper().setPhone(message);
        }
        // 发送短信
        SendSMS.message(phone,"【行业场所管理平台】验证码："+code+"(有效期为5分钟)，请勿泄露给他人，如非本人操作，请忽略此信息。");


    }

    @Override
    public Message phone(String username) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Message::getPhone,username);
        Message message = getBaseMapper().selectOne(wrapper);
        return  message;
    }

    @Override
    public void sendIsolation(String phone) throws Exception {
        if (StringUtils.isEmpty(phone)) {
            throw new Exception("手机号码不能为空");
        }
        if (phone.length() != 11){
            throw new Exception("手机号码不正确");
        }
        Random random = new Random();
        int code = random.nextInt(9999 - 1000 + 1) + 1000;
        System.out.println(code);
        String messageCode = DigestUtils.sha1Hex(String.valueOf(code));
        Message message = phone(phone);
        if(message ==null){
            message = new Message();
            message.setCode(messageCode);
            message.setPhone(phone);
            message.setCreateTime(LocalDateTime.now());
            message.setNumber(1);
            save(message);
        }else{
            Duration duration = Duration.between(message.getCreateTime(),LocalDateTime.now());
            if ((duration.toMinutes()<=1)) {
                throw new Exception("请等待一分钟再发起请求");
            }

            if (message.getNumber()>5) {
                throw new Exception("今日发送验证码次数已超过了5次");
            }
            if(LocalDateTimeUtil.isOnToday(message.getCreateTime())){
                message.setNumber(message.getNumber()+1);
            }else{
                message.setNumber(1);
            }
            message.setCode(messageCode);
            message.setCreateTime(LocalDateTime.now());
            getBaseMapper().setPhone(message);
        }
        // 发送短信
        SendSMS.message(phone,"【隔离场所管理平台】验证码："+code+"(有效期为5分钟)，请勿泄露给他人，如非本人操作，请忽略此信息。");


    }
}
